package com.geekcoding.geekoj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.geekcoding.common.dto.ApiResponse;
import com.geekcoding.common.dto.BasePageQuery;
import com.geekcoding.common.enums.State;
import com.geekcoding.geekoj.dto.QuestionDTO;
import com.geekcoding.geekoj.dto.TestDetailDTO;
import com.geekcoding.geekoj.dto.request.QuestionAnswerReq;
import com.geekcoding.geekoj.entity.Test;
import com.geekcoding.geekoj.entity.UserQuestionFavor;
import com.geekcoding.geekoj.nums.TestType;
import com.geekcoding.geekoj.repository.QuestionRepository;
import com.geekcoding.geekoj.service.TestService;
import com.geekcoding.geekoj.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController()
@RequestMapping("/api/test")
@RequiredArgsConstructor
public class TestController {

    private final TestService testService;

    private final UserService userService;

    private final QuestionRepository questionRepository;

    @PostMapping("create/{subjectCode}/{type}")
    public ApiResponse create(@PathVariable TestType type, @PathVariable String subjectCode) {
        Test test = testService.create(type, subjectCode, userService.currentUser());
        return ApiResponse.success(test);
    }

    /**
     * 下一题
     * @param testId
     * @return
     */
    @PostMapping("{testId}/nextQuestion")
    public ApiResponse nextQuestion(@PathVariable String testId){
        QuestionDTO question = testService.nextQuestion(testId, userService.currentUser());
        return ApiResponse.success(question);
    }

    /**
     * 测试详情
     * @param testId
     * @return
     */
    @PostMapping("{testId}/detail")
    public ApiResponse testDetail(@PathVariable String testId){
        TestDetailDTO testDetail = testService.testDetail(testId, userService.currentUser());
        return ApiResponse.success(testDetail);
    }

    /**
     * 题目收藏
     */
    @PostMapping("{questionId}/favor/{state}")
    public ApiResponse questionFavor(@PathVariable String questionId, @PathVariable State state) {
        UserQuestionFavor userQuestionFavor = testService.questionFavor(questionId, userService.currentUser(), state);
        return ApiResponse.success(userQuestionFavor);
    }


    @GetMapping("question/favor/{questionId}")
    public ApiResponse userQuestionFavor(@PathVariable String questionId) {
        UserQuestionFavor userQuestionFavor = testService.userFavor(questionId, userService.currentUser());
        return ApiResponse.success(userQuestionFavor);
    }

    // 提交答案
    @PostMapping("{testId}/question/{questionId}/answerSubmit")
    public ApiResponse questionAnswerSubmit(@PathVariable String testId, @PathVariable String questionId, @RequestBody QuestionAnswerReq req){
        QuestionDTO questionDTO = testService.submit(testId, questionId, req.getAnswerValue(), userService.currentUser());
        return ApiResponse.success(questionDTO);
    }

    @PostMapping("question/{questionId}/detail")
    public ApiResponse questionDetail(@PathVariable String questionId){
        return ApiResponse.success(QuestionDTO.convert(testService.questionDetail(questionId), null));
    }

    // 收藏题目列表
    @PostMapping("questionFavor/page")
    public ApiResponse favorList(@RequestBody BasePageQuery query) {
        Page<QuestionDTO> tPage = testService.queryFavorByPage(Page.of(query.getCurrent(), query.getPageSize()), userService.currentUser());
        return ApiResponse.success(tPage);
    }

    // 错题集列表
    @PostMapping("error/page")
    public ApiResponse errorQuestPage(@RequestBody BasePageQuery query){
        Page<QuestionDTO> tPage = testService.queryErrorByPage(Page.of(query.getCurrent(), query.getPageSize()), userService.currentUser());
        return ApiResponse.success(tPage);
    }

    @PostMapping("answer/{answerId}/detail")
    public ApiResponse answerDetail(@PathVariable String answerId){
        return ApiResponse.success(testService.answerDetail(answerId));
    }

    @GetMapping("ranking/list")
    public ApiResponse ranking() {
        return ApiResponse.success(testService.ranking());
    }

    @PostMapping("history/page")
    public ApiResponse historyPage(@RequestBody BasePageQuery query){
        Page<QuestionDTO> tPage = testService.historyPage(Page.of(query.getCurrent(), query.getPageSize()), userService.currentUser());
        return ApiResponse.success(tPage);
    }


    @GetMapping("personal/data")
    public ApiResponse statistic(){
        List<Test> testTop5List = testService.personalTop5(userService.currentUser());
        return ApiResponse.success(testTop5List);
    }

}
